package com.angzh.bolibao.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.tomcat.util.digester.SetRootRule;

import weixin.SnsUserInfoDemo;
import weixin.WebAccessToken;

import com.angzh.bolibao.common.Util;
import com.angzh.bolibao.model.Clicks;
import com.angzh.bolibao.model.Commission;
import com.angzh.bolibao.model.Customer;
import com.angzh.bolibao.model.User;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

public class CustomerController extends Controller{
	
	public void list(){
		List<User> companys = User.dao.companyList();
		setAttr("companyList",companys);
		String company = "";
		if(companys!=null&&!companys.isEmpty()){
			String user = companys.get(0).get("company");
			company= getPara("company")==null?user:getPara("company");
		}
		setAttr("initcompany",company);
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		int isCompensation = Integer.parseInt(getPara("isCompensation")==null?"-1":getPara("isCompensation"));
		int pageNum = Integer.parseInt(getPara("pageNum")==null?"1":getPara("pageNum"));
		int pageSize = Integer.parseInt(getPara("pageSize")==null?"12":getPara("pageSize"));
		setAttr("customer", Customer.dao.paginate(pageNum, pageSize, startTime, endTime, isCompensation,3,company));
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		setAttr("isCompensation",isCompensation);
		render("/pages/group/customer.jsp");
	}
	public void companylist(){
		HttpSession session = getSession(true);
		User user = (User)session.getAttribute("admin");
		int role = user.getInt("role");
		String company = user.getStr("company");
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		int isCompensation = Integer.parseInt(getPara("isCompensation")==null?"-1":getPara("isCompensation"));
		int pageNum = Integer.parseInt(getPara("pageNum")==null?"1":getPara("pageNum"));
		int pageSize = Integer.parseInt(getPara("pageSize")==null?"12":getPara("pageSize"));
		setAttr("customer", Customer.dao.paginate(pageNum, pageSize, startTime, endTime, isCompensation,role,company));
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		setAttr("isCompensation",isCompensation);
		render("/pages/company/customer.jsp");
	}
	public void listbyworker(){
		HttpSession session = getSession(true);
		User user = (User)session.getAttribute("admin");
		String weichat = user.getStr("weichat");
		int pageNum = Integer.parseInt(getPara("pageNum")==null?"1":getPara("pageNum"));
		int pageSize = Integer.parseInt(getPara("pageSize")==null?"12":getPara("pageSize"));
		setAttr("customer", Customer.dao.paginate(pageNum, pageSize,weichat));
		render("/pages/worker/customer.jsp");
	}
	/**
	 * 
	 * 子公司统计
	 * 
	 * **/
	public void companyCommission(){
		List list = new ArrayList();
		HttpSession session = getSession(true);
		User user = (User)session.getAttribute("admin");
		String company = user.getStr("company");
		int role = user.getInt("role");
		List<User> userlist = User.dao.userList(3, company);
		setAttr("userlist",userlist);
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		String weichat = getPara("weichat")==null?"":getPara("weichat");
		int total200=0,total400=0,total1000=0,totalclicks=0;
		float totalprice = 0,totalCommission=0;
		if(weichat.equals("")){
			for(int i=0;i<userlist.size();i++){
				weichat = userlist.get(i).getStr("weichat");
				if(weichat!=null && !weichat.equals("")){
					Map<String,Object> map = new HashMap<String,Object>();
					map = this.getTotalCommission(startTime, endTime, weichat);
					total200 += Integer.parseInt(map.get("type1num").toString());
					total400 += Integer.parseInt(map.get("type2num").toString());
					total1000 += Integer.parseInt(map.get("type3num").toString());
					totalprice += Float.parseFloat(map.get("totalprice").toString());
					totalCommission += Float.parseFloat(map.get("totalcommission").toString());
					int clicks = Clicks.dao.getClicks(weichat, startTime, endTime).getNumber("count").intValue();
					totalclicks += clicks;
					map.put("user", userlist.get(i).get("name"));
					map.put("clicks", clicks);
					list.add(map);
				}
			}
			Map<String,Object> mapAll = new HashMap<String,Object>();
			mapAll.put("user", "员工合计");
			mapAll.put("startTime", startTime);
			mapAll.put("endTime", endTime);
			
			mapAll.put("type1num", total200);
			mapAll.put("type2num", total400);
			mapAll.put("type3num", total1000);
			mapAll.put("totalprice", totalprice);
			mapAll.put("totalcommission", totalCommission);
			mapAll.put("clicks", totalclicks);
			list.add(mapAll);
			Commission c2 = Commission.dao.getCommission(1, -1);
			float percent11 = 0;
			float percent22 = 0;
			float percent33 = 0;
			if(c2!=null){
				percent11 = c2.getFloat("percent1")==null?0:c2.getFloat("percent1");
				percent22 = c2.getFloat("percent2")==null?0:c2.getFloat("percent2");
				percent33 = c2.getFloat("percent3")==null?0:c2.getFloat("percent3");
				totalCommission = 200 * total200 * percent11 + 400 * total400 * percent22 + 1000*total1000*percent33;
			}
			Map<String,Object> mapOwn = new HashMap<String,Object>();
			mapOwn.put("user", "公司合计");
			mapOwn.put("startTime", startTime);
			mapOwn.put("endTime", endTime);
			
			mapOwn.put("type1num", total200);
			mapOwn.put("type2num", total400);
			mapOwn.put("type3num", total1000);
			mapOwn.put("totalprice", totalprice);
			mapOwn.put("totalcommission", totalCommission);
			mapAll.put("clicks", totalclicks);
			list.add(mapOwn);
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map = this.getTotalCommission(startTime, endTime, weichat);
			User worker = User.dao.getUserByWeichat(weichat);
			map.put("user", worker.get("name"));
			int clicks = Clicks.dao.getClicks(weichat, startTime, endTime).getNumber("count").intValue();
			map.put("clicks", clicks);
			list.add(map);
		}
		
		setAttr("list",list);
		if(role==1){
			render("/pages/group/statisticsWorker.jsp");
		}else if(role==2)
			render("/pages/company/statistics.jsp");
	}
	//导出员工佣金统计到excel
	public void export(){
		Date d=new Date();  
	    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");  
	    String title=dateFormat.format(d);  
	    System.out.println(title);  
	    File file = new File(title+"_统计表"+".xls");  
	    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();  
        // sheet:一张表的简称  
        // row:表里的行  
        // 创建工作薄中的工作表  
        HSSFSheet hssfSheet = hssfWorkbook.createSheet("test");  
        // 创建表头行  
        HSSFRow rowHead = hssfSheet.createRow(0);  
        // 创建单元格，设置表头 创建列  
        CellStyle style = hssfWorkbook.createCellStyle();//新建样式对象
        style.setBorderBottom(HSSFColor.BLUE.index);
        style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
        rowHead.createCell(0).setCellValue("员工名字");
        rowHead.getCell(0).setCellStyle(style);
        rowHead.createCell(1).setCellValue("开始时间");
        rowHead.getCell(1).setCellStyle(style);
        rowHead.createCell(2).setCellValue("结束时间");
        rowHead.getCell(2).setCellStyle(style);
        rowHead.createCell(3).setCellValue("200元单数");
        rowHead.getCell(3).setCellStyle(style);
        rowHead.createCell(4).setCellValue("500元单数");
        rowHead.getCell(4).setCellStyle(style);
        rowHead.createCell(5).setCellValue("1000元单数");
        rowHead.getCell(5).setCellStyle(style);
        rowHead.createCell(6).setCellValue("成交金额");
        rowHead.getCell(6).setCellStyle(style);
        rowHead.createCell(7).setCellValue("所得佣金");
        rowHead.getCell(7).setCellStyle(style);
        
        List list = new ArrayList();
		HttpSession session = getSession(true);
		User user = (User)session.getAttribute("admin");
		String company = user.getStr("company");
		int role = user.getInt("role");
		List<User> userlist = User.dao.userList(3, company);
		//setAttr("userlist",userlist);
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		//setAttr("startTime",startTime);
		//setAttr("endTime",endTime);
		String weichat = getPara("weichat")==null?"":getPara("weichat");
		int total200=0,total400=0,total1000=0;
		float totalprice = 0,totalCommission=0;
		int j=1;
		if(weichat.equals("")){
			for(int i=0;i<userlist.size();i++){
				weichat = userlist.get(i).getStr("weichat");
				if(weichat!=null && !weichat.equals("")){
					HSSFRow row = hssfSheet.createRow(j);  
					Map<String,Object> map = new HashMap<String,Object>();
					map = this.getTotalCommission(startTime, endTime, weichat);
					total200 += Integer.parseInt(map.get("type1num").toString());
					total400 += Integer.parseInt(map.get("type2num").toString());
					total1000 += Integer.parseInt(map.get("type3num").toString());
					totalprice += Float.parseFloat(map.get("totalprice").toString());
					totalCommission += Float.parseFloat(map.get("totalcommission").toString());
					//map.put("user", userlist.get(i).get("name"));
					row.createCell(0).setCellValue(userlist.get(i).get("name")+"");
					row.createCell(1).setCellValue(startTime);
					row.createCell(2).setCellValue(endTime);
					row.createCell(3).setCellValue(map.get("type1num")+"");
					row.createCell(4).setCellValue(map.get("type2num")+"");
					row.createCell(5).setCellValue(map.get("type3num")+"");
					row.createCell(6).setCellValue(map.get("totalprice")+"");
					row.createCell(7).setCellValue(map.get("totalcommission")+"");
					j++;
				}
			}
			Map<String,Object> mapAll = new HashMap<String,Object>();
			HSSFRow rowt = hssfSheet.createRow(j);  
			//mapAll.put("user", "员工合计");
			rowt.createCell(0).setCellValue("员工合计");
			//mapAll.put("startTime", startTime);
			rowt.createCell(1).setCellValue(startTime);
			//mapAll.put("endTime", endTime);
			rowt.createCell(2).setCellValue(endTime);
			//mapAll.put("type1num", total200);
			rowt.createCell(3).setCellValue(total200);
			//mapAll.put("type2num", total500);
			rowt.createCell(4).setCellValue(total400);
			rowt.createCell(4).setCellValue(total1000);
			//mapAll.put("totalprice", totalprice);
			rowt.createCell(5).setCellValue(totalprice);
			//mapAll.put("totalcommission", totalCommission);
			rowt.createCell(6).setCellValue(totalCommission);
			//list.add(mapAll);
			j++;
			Commission c2 = Commission.dao.getCommission(1, -1);
			float percent11 = 0;
			float percent22 = 0;
			float percent33 = 0;
			if(c2!=null){
				percent11 = c2.getFloat("percent1")==null?0:c2.getFloat("percent1");
				percent22 = c2.getFloat("percent2")==null?0:c2.getFloat("percent2");
				percent33 = c2.getFloat("percent3")==null?0:c2.getFloat("percent3");
				totalCommission = 200 * total200 * percent11 + 400 * total400 * percent22+1000*total1000*percent33;
			}
//			Map<String,Object> mapOwn = new HashMap<String,Object>();
//			mapOwn.put("user", "公司合计");
//			mapOwn.put("startTime", startTime);
//			mapOwn.put("endTime", endTime);
//			
//			mapOwn.put("type1num", total200);
//			mapOwn.put("type2num", total500);
//			mapOwn.put("totalprice", totalprice);
//			mapOwn.put("totalcommission", totalCommission);
			HSSFRow rowl = hssfSheet.createRow(j);  
			rowl.createCell(0).setCellValue("公司合计");
			rowl.createCell(1).setCellValue(startTime);
			rowl.createCell(2).setCellValue(endTime);
			rowl.createCell(3).setCellValue(total200);
			rowl.createCell(4).setCellValue(total400);
			rowl.createCell(5).setCellValue(total1000);
			rowl.createCell(6).setCellValue(totalprice);
			rowl.createCell(7).setCellValue(totalCommission);
			
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map = this.getTotalCommission(startTime, endTime, weichat);
			User worker = User.dao.getUserByWeichat(weichat);
			map.put("user", worker.get("name"));
			HSSFRow row = hssfSheet.createRow(j);  
			row.createCell(0).setCellValue(worker.get("name")+"");
			row.createCell(1).setCellValue(startTime);
			row.createCell(2).setCellValue(endTime);
			row.createCell(3).setCellValue(map.get("type1num")+"");
			row.createCell(4).setCellValue(map.get("type2num")+"");
			row.createCell(5).setCellValue(map.get("type3num")+"");
			row.createCell(6).setCellValue(map.get("totalprice")+"");
			row.createCell(7).setCellValue(map.get("totalcommission")+"");
		}
		 try {  
	            FileOutputStream fileOutputStreane = new FileOutputStream(file);  
	            hssfWorkbook.write(fileOutputStreane);  
	            fileOutputStreane.flush();  
	            fileOutputStreane.close();  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
		 renderFile(file);
	}
	public Map<String, Object> getTotalCommission(String startTime,String endTime,String weichat){
		
		List<Customer> customer1 = Customer.dao.totalCommission(startTime, endTime, weichat, 1);
		int type1num = 0;
		if(customer1!=null){
			type1num = customer1.size();
		}
		List<Customer> customer2 = Customer.dao.totalCommission(startTime, endTime, weichat, 2);
		int type2num = 0;
		if(customer2!=null){
			type2num= customer2.size();
		}
		List<Customer> customer3 = Customer.dao.totalCommission(startTime, endTime, weichat, 3);
		int type3num = 0;
		if(customer3!=null){
			type3num= customer3.size();
		}
		float totalprice = type1num * 200 + type2num * 400 + type3num * 1000;
		HttpSession session = getSession(true);
		User user = (User)session.getAttribute("admin");
		int companyId = user.getInt("id");
		Commission c = Commission.dao.getCommission(2,companyId);
		float percent1 = 0;
		float percent2 = 0;
		float percent3 = 0;
		if(c!=null){
			percent1 = c.getFloat("percent1")==null?0:c.getFloat("percent1");
			percent2 = c.getFloat("percent2")==null?0:c.getFloat("percent2");
			percent3 = c.getFloat("percent3")==null?0:c.getFloat("percent3");
		}
		float totalCommission = type1num * 200 * percent1  + type2num * 400 * percent2  + type3num*1000*percent3;
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("type1num", type1num);
		map.put("type2num", type2num);
		map.put("type3num", type3num);
		map.put("totalprice", totalprice);
		map.put("totalcommission", totalCommission);
		return map;
	}
	public Map<String, Object> getTotalCommission2(String startTime,String endTime,String weichat,String companyname){
		
		List<Customer> customer1 = Customer.dao.totalCommission(startTime, endTime, weichat, 1);
		int type1num = 0;
		if(customer1!=null){
			type1num = customer1.size();
		}
		List<Customer> customer2 = Customer.dao.totalCommission(startTime, endTime, weichat, 2);
		int type2num = 0;
		if(customer2!=null){
			type2num= customer2.size();
		}
		List<Customer> customer3 = Customer.dao.totalCommission(startTime, endTime, weichat, 3);
		int type3num = 0;
		if(customer3!=null){
			type3num= customer3.size();
		}
		float totalprice = type1num * 200 + type2num * 400 + type3num * 1000;
		User company = User.dao.getCompanyByName(2, companyname);
		int companyId = company.getInt("id");
		Commission c = Commission.dao.getCommission(2,companyId);
		float percent1 = 0;
		float percent2 = 0;
		float percent3 = 0;
		if(c!=null){
			percent1 = c.getFloat("percent1")==null?0:c.getFloat("percent1");
			percent2 = c.getFloat("percent2")==null?0:c.getFloat("percent2");
			percent3 = c.getFloat("percent3")==null?0:c.getFloat("percent3");
		}
		float totalCommission = type1num * 200 * percent1  + type2num * 400 * percent2  + type3num*1000*percent3;
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("type1num", type1num);
		map.put("type2num", type2num);
		map.put("type3num", type3num);
		map.put("totalprice", totalprice);
		map.put("totalcommission", totalCommission);
		return map;
	}
	//手机提交支付表单
	public void saveCustomer(){
//		String code = getPara("code");
//		String state = getPara("state");
		Customer c = getModel(Customer.class);
		String license = c.get("license").toString();
		int comptype = Integer
				.parseInt(c.get("isCompensation").toString()==null||c.get("isCompensation").toString().equals("")?"0":c.get("isCompensation").toString());
		List list = Customer.dao.getByLicense(license,comptype);
		if (list != null && list.size() > 0 || license.equals("")) {
			render("/payerror.jsp");
			//redirect("/weixin/payfail");
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String createTime = sdf.format(date);
			c.set("buytime", createTime);
			
			Calendar ca1 = Calendar.getInstance();  
			ca1.add(Calendar.DATE, 10);
			Date effcdate = ca1.getTime();  
			String effctime = sdf.format(effcdate);  
			c.set("effctime", effctime);
			
			Calendar ca2 = Calendar.getInstance();
			ca2.add(Calendar.DATE, 10);
			ca2.add(Calendar.YEAR, 3);
			Date safedate = ca2.getTime();
			String safetime = sdf.format(safedate); 
			c.set("safetime", safetime);
			
			c.set("isCompensation", 0);
			c.save();
			
			Integer id = c.getInt("id");
			Customer c2 = Customer.dao.findFirst("select * from customer where id = ? ", id); 
			setSessionAttr("cstm",c2);
	//		WebAccessToken webAccessToken = SnsUserInfoDemo.getWebAccessToken(code);
	//		if(webAccessToken != null){
	//			String openId = webAccessToken.getOpenid();
	//			setAttr("openId",openId);
	//			setAttr("body","玻璃宝-"+c2.get("price"));
	//			setAttr("out_trade_no",c2.get("id"));
	//			//redirect("/weixin/indexWithPara?openId="+openId);
	//		}else{
	//			System.out.println("获取access_token失败");
	//		}
			
//			render("/paysuccess.jsp");
			redirect("/payfinish.jsp");
		}
	}
	//手机提交赔付表单
	public void peifu(){
		Date date = new Date();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileNameDir = sdf2.format(date);
		PathKit  kit = new PathKit();
		@SuppressWarnings("static-access")
		String contextPath = kit.getWebRootPath();
		String path = "/upload/"+fileNameDir;
		String pathUrl = contextPath+path;
		UploadFile file = getFile("customerpic", pathUrl);
		String license = getPara("license");
		int comptype = getParaToInt("isCompensation");
		List<Customer> l = Customer.dao.getByLicense(license, comptype);
		if(l!=null && !l.isEmpty()){
			render("/peifufail.jsp");
		}else {
			List<Customer> l2 = Customer.dao.getByLicense(license, 0);
			Customer c = l2.get(0);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			String comptime = sdf.format(date);
			c.set("comptime", comptime);
			c.set("isCompensation", 1);
			
			String fileName = file.getOriginalFileName();
			c.set("customerpic", path+File.separator+fileName);
			c.update();
			setAttr("code",c.get("phone"));
			render("/peifusuccess.jsp");
		}
		
	}
	//赔付申请列表
	public void peifulist(){
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		String phone = getPara("phone")==null?"":getPara("phone");
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		setAttr("phone",phone);
		int pageNum = Integer.parseInt(getPara("pageNum")==null?"1":getPara("pageNum"));
		int pageSize = Integer.parseInt(getPara("pageSize")==null?"12":getPara("pageSize"));
		Page<Customer> c = Customer.dao.getCustomerList(pageNum, pageSize,startTime,endTime,phone, 1);
		setAttr("customerList",c);
		render("/pages/service/beforeList.jsp");
	}
	//赔付列表导出
	public void beforeexport(){
		Date d=new Date();  
	    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");  
	    String title=dateFormat.format(d);  
	    System.out.println(title);  
	    File file = new File(title+"_统计表"+".xls");  
	    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();  
        // sheet:一张表的简称  
        // row:表里的行  
        // 创建工作薄中的工作表  
        HSSFSheet hssfSheet = hssfWorkbook.createSheet("test");  
        // 创建表头行  
        HSSFRow rowHead = hssfSheet.createRow(0);  
        // 创建单元格，设置表头 创建列  
        CellStyle style = hssfWorkbook.createCellStyle();//新建样式对象
        style.setBorderBottom(HSSFColor.BLUE.index);
        style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
        rowHead.createCell(0).setCellValue("客户姓名");
        rowHead.getCell(0).setCellStyle(style);
        rowHead.createCell(1).setCellValue("联系方式");
        rowHead.getCell(1).setCellStyle(style);
        rowHead.createCell(2).setCellValue("车牌");
        rowHead.getCell(2).setCellStyle(style);
        rowHead.createCell(3).setCellValue("参保价格");
        rowHead.getCell(3).setCellStyle(style);
        rowHead.createCell(4).setCellValue("申请赔付时间");
        rowHead.getCell(4).setCellStyle(style);
        String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		String phone = getPara("phone")==null?"":getPara("phone");
		List<Customer> c = Customer.dao.getCustomerToList(startTime,endTime,phone, 1);
		if(c!=null && !c.isEmpty()){
			for(int i=0;i<c.size();i++){
				 HSSFRow row = hssfSheet.createRow(i+1);  
				 row.createCell(0).setCellValue(c.get(i).get("name")+"");
				 row.createCell(1).setCellValue(c.get(i).get("phone")+"");
				 row.createCell(2).setCellValue(c.get(i).get("license")+"");
				 row.createCell(3).setCellValue(c.get(i).get("price")+"");
				 row.createCell(4).setCellValue(c.get(i).get("comptime")+"");
			}
		}
		 try {  
	            FileOutputStream fileOutputStreane = new FileOutputStream(file);  
	            hssfWorkbook.write(fileOutputStreane);  
	            fileOutputStreane.flush();  
	            fileOutputStreane.close();  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
		 renderFile(file);
	}
	//受理赔付
	public void doPeifu(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String completetime = sdf2.format(date);
		String fileNameDir = sdf.format(date);
		PathKit  kit = new PathKit();
		@SuppressWarnings("static-access")
		String contextPath = kit.getWebRootPath();
		String path = "/upload/"+fileNameDir;
		String pathUrl = contextPath+path;
		
		UploadFile beforefile = getFile("beforepic",pathUrl);
		UploadFile afterfile = getFile("afterpic",pathUrl);
		String beforeName = beforefile.getOriginalFileName();
		String afterName = afterfile.getOriginalFileName();
		int id = getParaToInt("id");
		Customer c = Customer.dao.findById(id);
		
		c.set("beforepic", pathUrl+File.separator+beforeName);
		c.set("afterpic",pathUrl+File.separator+afterName);
		c.set("isCompensation", 2);
		c.set("completetime", completetime);
		c.update();
		render("/pages/service/beforeList.jsp");
	}
	//赔付完成列表
	public void afterpeifuList(){
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		String phone = getPara("phone")==null?"":getPara("phone");
		int pageNum = Integer.parseInt(getPara("pageNum")==null?"1":getPara("pageNum"));
		int pageSize = Integer.parseInt(getPara("pageSize")==null?"12":getPara("pageSize"));
		Page<Customer> c = Customer.dao.getCustomerList(pageNum, pageSize,startTime,endTime,phone, 2);
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		setAttr("phone",phone);
		setAttr("customerList",c);
		render("/pages/service/afterList.jsp");
	}
	//完成赔付列表导出
	public void afterexport(){
		Date d=new Date();  
	    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");  
	    String title=dateFormat.format(d);  
	    System.out.println(title);  
	    File file = new File(title+"_统计表"+".xls");  
	    HSSFWorkbook hssfWorkbook = new HSSFWorkbook();  
        // sheet:一张表的简称  
        // row:表里的行  
        // 创建工作薄中的工作表  
        HSSFSheet hssfSheet = hssfWorkbook.createSheet("test");  
        // 创建表头行  
        HSSFRow rowHead = hssfSheet.createRow(0);  
        // 创建单元格，设置表头 创建列  
        CellStyle style = hssfWorkbook.createCellStyle();//新建样式对象
        style.setBorderBottom(HSSFColor.BLUE.index);
        style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
        rowHead.createCell(0).setCellValue("客户姓名");
        rowHead.getCell(0).setCellStyle(style);
        rowHead.createCell(1).setCellValue("联系方式");
        rowHead.getCell(1).setCellStyle(style);
        rowHead.createCell(2).setCellValue("车牌");
        rowHead.getCell(2).setCellStyle(style);
        rowHead.createCell(3).setCellValue("参保价格");
        rowHead.getCell(3).setCellStyle(style);
        rowHead.createCell(4).setCellValue("申请赔付时间");
        rowHead.getCell(4).setCellStyle(style);
        rowHead.createCell(5).setCellValue("完成赔付时间");
        rowHead.getCell(5).setCellStyle(style);
        String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		String phone = getPara("phone")==null?"":getPara("phone");
		List<Customer> c = Customer.dao.getCustomerToList(startTime,endTime,phone, 2);
		if(c!=null && !c.isEmpty()){
			for(int i=0;i<c.size();i++){
				 HSSFRow row = hssfSheet.createRow(i+1);  
				 row.createCell(0).setCellValue(c.get(i).get("name")+"");
				 row.createCell(1).setCellValue(c.get(i).get("phone")+"");
				 row.createCell(2).setCellValue(c.get(i).get("license")+"");
				 row.createCell(3).setCellValue(c.get(i).get("price")+"");
				 row.createCell(4).setCellValue(c.get(i).get("comptime")+"");
				 row.createCell(5).setCellValue(c.get(i).get("completetime")+"");
			}
		}
		 try {  
	            FileOutputStream fileOutputStreane = new FileOutputStream(file);  
	            hssfWorkbook.write(fileOutputStreane);  
	            fileOutputStreane.flush();  
	            fileOutputStreane.close();  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
		 renderFile(file);
	}
	//集团查看赔付状态
	public void listPeifuAll(){
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		String phone = getPara("phone")==null?"":getPara("phone");
		int pageNum = Integer.parseInt(getPara("pageNum")==null?"1":getPara("pageNum"));
		int pageSize = Integer.parseInt(getPara("pageSize")==null?"12":getPara("pageSize"));
		int isCompensation = Integer.parseInt(getPara("isCompensation")==null?"1":getPara("isCompensation"));
		Page<Customer> c = Customer.dao.getCustomerList(pageNum, pageSize,startTime,endTime,phone, isCompensation);
		setAttr("customerList",c);
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		setAttr("phone",phone);
		setAttr("isCompensation",isCompensation);
		render("/pages/group/peifulist.jsp");
	}
	//集团查看子公司员工统计
	public void worker4group(){
		List<User> companys = User.dao.companyList();
		setAttr("companyList",companys);
		String company = "";
		if(companys!=null&&!companys.isEmpty()){
			String user = companys.get(0).get("company");
			company= getPara("company")==null?user:getPara("company");
		}
		setAttr("initcompany",company);
		List list = new ArrayList();
		List<User> userlist = User.dao.userList(3, company);
		setAttr("userlist",userlist);
		String startTime = getPara("startTime")==null?"":getPara("startTime");
		String endTime = getPara("endTime")==null?"":getPara("endTime");
		setAttr("startTime",startTime);
		setAttr("endTime",endTime);
		String weichat = getPara("weichat")==null?"":getPara("weichat");
		int total200=0,total400=0,total1000=0,totalclicks=0;
		float totalprice = 0,totalCommission=0;
		if(weichat.equals("")){
			if(userlist!=null && !userlist.isEmpty()){
				for(int i=0;i<userlist.size();i++){
					weichat = userlist.get(i).getStr("weichat");
					company = userlist.get(i).getStr("company");
					if(weichat!=null && !weichat.equals("")){
						Map<String,Object> map = new HashMap<String,Object>();
						map = this.getTotalCommission2(startTime, endTime, weichat,company);
						total200 += Integer.parseInt(map.get("type1num").toString());
						total400 += Integer.parseInt(map.get("type2num").toString());
						total1000 += Integer.parseInt(map.get("type3num").toString());
						totalprice += Float.parseFloat(map.get("totalprice").toString());
						totalCommission += Float.parseFloat(map.get("totalcommission").toString());
						int clicks = Clicks.dao.getClicks(weichat, startTime, endTime).getNumber("count").intValue();
						totalclicks += clicks;
						map.put("user", userlist.get(i).get("name"));
						map.put("clicks", clicks);
						list.add(map);
					}
				}
			}
			Map<String,Object> mapAll = new HashMap<String,Object>();
			mapAll.put("user", "员工合计");
			mapAll.put("startTime", startTime);
			mapAll.put("endTime", endTime);
			
			mapAll.put("type1num", total200);
			mapAll.put("type2num", total400);
			mapAll.put("type3num", total1000);
			mapAll.put("totalprice", totalprice);
			mapAll.put("totalcommission", totalCommission);
			mapAll.put("clicks", totalclicks);
			list.add(mapAll);
			Commission c2 = Commission.dao.getCommission(1, -1);
			float percent11 = 0;
			float percent22 = 0;
			float percent33 = 0;
			if(c2!=null){
				percent11 = c2.getFloat("percent1")==null?0:c2.getFloat("percent1");
				percent22 = c2.getFloat("percent2")==null?0:c2.getFloat("percent2");
				percent33 = c2.getFloat("percent3")==null?0:c2.getFloat("percent3");
				totalCommission = 200 * total200 * percent11 + 400 * total400 * percent22 + 1000*total1000*percent33;
			}
			Map<String,Object> mapOwn = new HashMap<String,Object>();
			mapOwn.put("user", "公司合计");
			mapOwn.put("startTime", startTime);
			mapOwn.put("endTime", endTime);
			
			mapOwn.put("type1num", total200);
			mapOwn.put("type2num", total400);
			mapOwn.put("type3num", total1000);
			mapOwn.put("totalprice", totalprice);
			mapOwn.put("totalcommission", totalCommission);
			mapAll.put("clicks", totalclicks);
			list.add(mapOwn);
		}else{
			Map<String,Object> map = new HashMap<String,Object>();
			map = this.getTotalCommission(startTime, endTime, weichat);
			User worker = User.dao.getUserByWeichat(weichat);
			map.put("user", worker.get("name"));
			int clicks = Clicks.dao.getClicks(weichat, startTime, endTime).getNumber("count").intValue();
			map.put("clicks", clicks);
			list.add(map);
		}
		setAttr("list",list);
		render("/pages/group/worker4group.jsp");
	}
	public void listUserbyCompany(){
		String company = getPara("company");
		List<User>userList = User.dao.userList(3, company);
		Map<String, Object> retJson = Util.getRetJson();
		retJson = Util.getOkRetJson("获取成功");
		retJson.put("userList", userList);
		renderJson(retJson);
	}
}
